package com.itheima.springboot_enable;

import com.itheima.config.EnableUser;
import com.itheima.config.MyImportBeanDefinitionRegistrar;
import com.itheima.config.MyImportSelector;
import com.itheima.config.UserConfig;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * @ComponentScan 扫描范围，当前引导类所在的包及其子包
 *
 * com.itheima.config;
 * com.itheima.springboot_enable;
 * 以上两个没有包含关系，所以加载不到
 * 方法1：可以使用@ComponentScan可以扫描到com.itheima.config包
 * 方法2：可以使用@Import注解，加载类。这些类都会被Spring创建并导入IOC容器
 * 方法3：可以对@Import注解进行封装。
 */


@SpringBootApplication
//@ComponentScan("com.itheima.config")
//@Import(UserConfig.class)
//@EnableUser

/**
 * Import的四种用法
 * 1.导入bean
 * 2.导入配置类
 * 3.导入ImportSelector的实现类
 * 4.导入ImportBeanDefinitionRegistrar的实现类
 *
 */

//@Import(User.class)
//@Import(UserConfig.class)
//@Import(MyImportSelector.class)
@Import(MyImportBeanDefinitionRegistrar.class)
public class SpringbootEnableApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);

        //获取Bean
        /*Object user = context.getBean("user");
        System.out.println(user);*/

        /*User user = context.getBean(User.class);
        System.out.println(user);*/

        /*Map<String, User> map = context.getBeansOfType(User.class);
        System.out.println(map);*/

        /*Role role = context.getBean(Role.class);
        System.out.println(role);*/

        Jedis jedis = context.getBean(Jedis.class);
        System.out.println(jedis);

        //在redis种存取数据
        jedis.set("name","赵亚男");
        String name = jedis.get("name");
        System.out.println(name);
    }

    @Bean
    public Jedis jedis(){
        return new Jedis("localhost",6379);
    }

}
